import java.io.File;

import desposito6.utils.CommandLine;
import scfg.TrainingSet;
import scfg.TrainingSet.GrammarType;
import scfg.output.CommandLineMenu;
import scfg.output.Display;
import scfg.utils.trainging_results.StatsCollector;

public class Main {

	private static Display output;

	static {
		output = new Display("JSCFG_Main.java");
	}
	
	private static void processPrepareRnaFiles(String[] args, boolean verbose){
		
	}
	
	private static void processTraining(String[] args,GrammarType type, boolean verbose){
		int temp = CommandLine.containsArg(args, "-tf");
		if (temp >= 0 && temp < args.length) {
			File tf = new File(args[temp + 1]);
			if (tf.exists()) {
				TrainingSet ts = new TrainingSet(output);
				ts.trainingSetFile(tf, type, new StatsCollector(), verbose);
			}
		}
		temp = CommandLine.containsArg(args, "-tfd");
		if (temp >= 0 && temp < args.length) {
			TrainingSet ts = new TrainingSet(output);
			ts.trainingSetFileDirectory(args[temp+1], ".txt", type, verbose);
		}
		temp = CommandLine.containsArg(args, "-td");
		if (temp >= 0 && temp < args.length) {
			TrainingSet ts = new TrainingSet(output);
			ts.trainingSetDirectory(args[temp+1], type, verbose);
		}
	}

	private static void displayHelpMenu() {
		String program = "JSCFG";
		String description = "J(ava) S(tochastic) C(ontext) F(ree) G(rammar) "
				+ "is a program to...";
		String comments = "Please contact David Esposito: "
				+ "desposito6[at]gatech.edu with and problems or questions.";
		CommandLineMenu cm = new CommandLineMenu(program, description, comments);
		cm.add("-h(elp)", "Display help menu");
		cm.add("-tf [file]", "Specifies the t(raining) f(ile) which contains a list "
				+ "of \".rna\" files inside the training set.");
		cm.add("-tfd [directory]", "Specifies the t(raining) f(ile) d(irectory) which "
				+ "contains multiple training files. Each file represents a training set"
				+ " containing a list of \".rna\" files  inside the training set. Recursive.");
		cm.add("-td [directory]", "Specifies the t(raining) d(irectory) which contains \".rna\" files."
				+ " The directory is considered a training set. Non-recursive.");
		cm.add("-v", "Show verbose output");
		cm.add("-o", "Specifies the name of the output file.");
		cm.add("-g [type]", "Specifies grammar type. Default: pfold, Optional: kh.");
		System.out.println(cm);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (CommandLine.containsArg(args, "-h") >= 0 || CommandLine.containsArg(args, "-help") >= 0){
			displayHelpMenu();
			System.exit(0);
		}
		boolean verbose = CommandLine.containsArg(args, "-v") >= 0;
		int temp = CommandLine.containsArg(args, "-o");
		if (temp >= 0 && temp < args.length)
			output = new Display(args[temp + 1]);
		temp = CommandLine.containsArg(args, "-g");
		GrammarType type = GrammarType.Pfold;
		if (temp >= 0 && temp < args.length){
			if (args[temp+1].equals("kh"))
				type = GrammarType.KnudsenHein;
			else if (args[temp+1].equals("pfold"))
				type = GrammarType.Pfold;
		}
		
		processPrepareRnaFiles(args, verbose);
		
		processTraining(args, type, verbose);
	}
}
